/**
 * Copyright 2018 orivil.com. All rights reserved.
 * Use of this source code is governed by a MIT-style
 * license that can be found at https://mit-license.org.
 */

package actions

import (
	"gitee.com/tugoer/orivil/mux"
	"gitee.com/tugoer/wechat-share/code/model"
)

var Channel = []*mux.Action{
	{
		Method: "GET", Route: "channel", Name: "Get Channel",
		Params: mux.Parameters{
			{
				In:mux.Query,
				Schema: &getChannel{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &getChannel{}
			ctx.WarnParse(p)
			c, err := model.GetChannel(p.ID)
			if err != nil {
				ctx.ErrWarning(err)
			}
			ctx.SendJson(c)
		},
	},
	{
		Method: "GET", Route: "select-channels", Name: "Get Select Channels",
		Handler: func(ctx *mux.Context) {
			cs, err := model.GetSelectChannels()
			if err != nil {
				ctx.ErrWarning(err)
			}
			ctx.SendJson(map[string]interface{}{
				"list": cs,
			})
		},
	},
	{
		Method: "GET", Route: "count-channels", Name: "Count Channels",
		Handler: func(ctx *mux.Context) {
			total := model.CountChannels()
			ctx.SendJson(map[string]interface{}{
				"total": total,
			})
		},
	},
	{
		Method: "GET", Route: "channels", Name: "Query Channels",
		Params:mux.Parameters{
			{
				In:mux.Query,
				Schema:&queryChannel{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &queryChannel{}
			ctx.WarnParse(p)
			cs, err := model.QueryChannels(p.Desc, p.Limit, p.Offset)
			if err != nil {
				ctx.ErrWarning(err)
			}
			ctx.SendJson(map[string]interface{}{
				"list": cs,
			})
		},
	},
	{
		Method: "POST", Route: "channel", Name: "Create Channels",
		Params:mux.Parameters{
			{
				In:mux.Form,
				Schema:&model.Channel{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &model.Channel{}
			ctx.WarnParse(p)
			err := p.Create()
			if err != nil {
				if err == model.ErrRecordExist {
					ctx.MsgWarning("该渠道已经存在", "")
				} else {
					ctx.ErrWarning(err)
				}
			} else {
				ctx.SendJson(p)
			}
		},
	},
	{
		Method: "PUT", Route: "close-selects", Name: "Close Selects",
		Params:mux.Parameters{
			{
				In:mux.Form,
				Schema:&closeSelects{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &closeSelects{}
			ctx.WarnParse(p)
			err := model.CloseSelects(p.IDs)
			if err != nil {
				ctx.ErrWarning(err)
			} else {
				ctx.MsgSuccess("更新成功", "")
			}
		},
	},
	{
		Method: "PUT", Route: "channel", Name: "Update Channel",
		Params:mux.Parameters{
			{
				In:mux.Form,
				Schema:&model.Channel{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &model.Channel{}
			ctx.WarnParse(p)
			err := p.Update()
			if err != nil {
				ctx.ErrWarning(err)
			} else {
				ctx.MsgSuccess("更新成功", "")
			}
		},
	},
	{
		Method: "DELETE", Route: "channel", Name: "Delete Channel",
		Params:mux.Parameters{
			{
				In:mux.Query,
				Schema:&delChannel{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &delChannel{}
			ctx.WarnParse(p)
			c := &model.Channel{ID: p.ID}
			err := c.Delete()
			if err != nil {
				ctx.ErrWarning(err)
			} else {
				ctx.MsgSuccess("删除成功", "")
			}
		},
	},
}

type getChannel struct {
	ID int `required:"ID 不能为空"`
}

type delChannel getChannel

type queryChannel struct {
	Limit int `required:"Limit 不能为空" num:"1<=x<=30" num-msg:"限制在1-30之间"`
	Offset int `required:"Offset 不能为空"`
	Desc bool
}

type closeSelects struct {
	IDs []int `required:"IDs 不能为空"`
}